package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@CrossOrigin("*")
@RequestMapping(value = "/admin/role")
@RestController
public class AdminRoleController {

    @Autowired
    ApplicationContext context; //spring的上下文对象

    @Autowired
    DtsRoleService roleService;

    @PostMapping("/permissions")
    public Object updatePermissions(@RequestBody Map<String, Object> map ){
        int roleId = (Integer) map.get("roleId");
        ArrayList<String> list = (ArrayList) map.get("permissions");
        String[] permissions = list.toArray(new String[]{});

        if(roleService.isSuperAdmin(roleId)){
            return ResponseUtil.ok();
        }

        roleService.updatePermission(roleId, permissions);

        return ResponseUtil.ok();
    }


    @GetMapping("/permissions")
    public Object permissions(Integer roleId){

        String basePackage = "com.qf.controller";

        //1. 查询系统权限， 基本思路，获取所有controller和控制器方法上的所有权限信息
        List<Permission> permissions = PermissionUtil.listPermission(context, basePackage);
        //组装数据
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);

        Set<String> assignedPermissions;
        //判断是否是超级管理员， 如果是超级管理员，把所有系统权限都配上
        if(roleService.isSuperAdmin(roleId)){
            //如果是，匹配所有的系统权限
            assignedPermissions = PermissionUtil.listPermissionString(permissions);
        }else{
            // assignedPermissions 该角色已有的权限
            assignedPermissions = roleService.findRoleStrsById(roleId);
        }


        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions", systemPermissions);
        data.put("assignedPermissions", assignedPermissions);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        roleService.deleteById(role.getId());
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsRole role){
        roleService.update(role);
        return ResponseUtil.ok();
    }


    @RequestMapping("/create")
    public Object add(@RequestBody DtsRole role){
        DtsRole dtsRole = roleService.add(role);
        return ResponseUtil.ok(dtsRole);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page ,
            @RequestParam(name = "limit", required = false, defaultValue = "20")Integer limit ,
            @RequestParam(name = "sort", required = false, defaultValue = "add_time")String sort ,
            @RequestParam(name = "order", required = false, defaultValue = "desc") String  order ,
            String rolename){

        IPage<DtsRole> ipage = roleService.queryPage(page, limit, sort, order, rolename);

        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);

    }


    @RequestMapping("/options")
    public Object findAll(){
        List<DtsRole> roleList =  roleService.findAll();

        List<Map<String, Object>> data = roleList.stream().map(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }
}
